(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  window.Region = (function() {
    function Region(map, elem, language) {
      var key, _i, _len, _ref;
      this.map = map;
      
      // the image map polygon from the markup
      this.elem = j$(elem);
      
      this.name = this.elem.attr('id');
      this.map.regions[this.name] = this;
      this.setLatLngMods();
      this.bindPolyClicked();

      //console.log("map.language = " + map.language);
      //console.log("<img class='zoomable' src='/Assets/" + map.language + "/images/maps/" + this.name + "_zoom.png' id='" + this.name + "-zoom' />");

      // create a small, light-weight image to show on region hover
      //this.hover = j$("<img id='" + this.name + "-hover' class='hoverable' src='/Assets/" + map.language + "/images/maps/" + this.name + "_hover.png' />");
      var hoverSrcStr = contextPath +  "/resources/portal/img/maps/" + this.name + "_hover.png";
      this.hover = j$("<img id='" + this.name + "-hover' class='hoverable' src='"+hoverSrcStr+"' />");
      this.hover.appendTo('#map');
      this.hover.dimensions = {};
      this.bindHover();
      
      // create zoomable image, initially hidden
//      this.zoomable = j$("<img class='zoomable' src='/Assets/" + map.language + "/images/maps/" + this.name + "_zoom.png' id='" + this.name + "-zoom' />");
      var zoomableSrcStr = contextPath + "/resources/portal/img/maps/" + this.name + "_zoom.png";
      this.zoomable = j$("<img class='zoomable' src='"+zoomableSrcStr+"' id='" + this.name + "-zoom' />");
      this.zoomable.appendTo('#map');
      
      // set references to original dimensions, from css
      this.zoomable.dimensions = {};
      _ref = ['width', 'height', 'top', 'left'];
      
      var hoverAra = this.elem.attr('data-hover').split(':');
      var zoomAra = this.elem.attr('data-zoom').split(':');
      
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        key = _ref[_i];
        
        this.hover.dimensions[key] = hoverAra[_i] + 'px';
        this.hover.dimensions[key + 'Num'] = Number(hoverAra[_i]);
        this.hover.css(key, this.hover.dimensions[key]);
        
        this.zoomable.dimensions[key] = zoomAra[_i] + 'px';
        this.zoomable.dimensions[key + 'Num'] = Number(zoomAra[_i]);
        this.zoomable.css(key, this.zoomable.dimensions[key]);
      }
    }
    
    // get x and y factors and offsets from the dom
    Region.prototype.setLatLngMods = function() {
      this.xoffset = Number(this.elem.attr('data-xoffset') || this.map.xoffset);
      this.xfactor = Number(this.elem.attr('data-xfactor') || this.map.xfactor);
      this.yoffset = Number(this.elem.attr('data-yoffset') || this.map.yoffset);
      return this.yfactor = Number(this.elem.attr('data-yfactor') || this.map.yfactor);
    };
    
    // zoom in on image map click
    Region.prototype.bindPolyClicked = function() {
      return this.elem.bind('click', __bind(function(e, point) {
        if (this.map.activeRegion) {
          return;
        }
        this.map.currentlyAnimating = true;
        this.map.hidePoints();
        this.map.activeRegion = this;
        
        // hide any other regions that may have been hovered over during animation
        this.map.blurRegions();
        this.hover.show();
        return this.zoomable.show().animate({
          width: this.map.width,
          height: this.map.height,
          top: 0,
          left: 0
        }, this.map.zoomSpeed, __bind(function() {
          
          // if a point was passed to the above click event,
          // consider it clicked too (after the animation finishes)
          // if (point) {
          //   point.focus();
          //   point.img.trigger('click');
          // }
          
          this.map.secondBreadcrumbLink.addClass('active');
          this.map.currentlyAnimating = false;
          
          // prevent image-map hover cursor below current layer
          this.map.varnish.attr('usemap', null);
          this.map.zoomOutLink.show();
          
          return this.map.drawPoints();
        }, this));
      }, this));
    };
    Region.prototype.show = function() {
      if (this.map.activeRegion) {
        return;
      }
      this.map.blurRegions();
      return this.hover.show();
    };
    Region.prototype.hide = function() {
      if (this !== this.map.activeRegion) {
        return this.hover.hide();
      }
    };
    Region.prototype.bindHover = function() {
      this.elem.bind('mouseout', __bind(function() {
        return this.hide();
      }, this));
      return this.elem.bind('mousemove', __bind(function() {
        if (this.map.currentlyAnimating) {
          return;
        }
        return this.show();
      }, this));
    };
    return Region;
  })();
}).call(this);
